using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace SIPSwitch.Web
{
    public enum SIPUserAgentsEnum
    {
        Unknown = 0,
        CiscoIPPhone = 1,
        PolycomIPPhone = 2,
        XtenSoftphone = 3,
        NetgearTA612V = 4,
        NetgearWGR613 = 5,
        LinksysPAP2 = 6,
        NokiaE61 = 7,
        GrandstreamGXP2000 = 8,
        LinksysRT31P2 = 9,
        LinksysSPA941 = 10,
        Sipura2100 = 11,
        SiemensGigaset = 12,
        DraytekVigor = 13,
        Asterisk = 14,
    }
    
    public class SIPUserAgents
    {
        public static SIPUserAgentsEnum GetUserAgent(string userAgentHeader)
        {
            SIPUserAgentsEnum userAgent = SIPUserAgentsEnum.Unknown;

            if (userAgentHeader != null && userAgentHeader.Trim().Length > 0)
            {
                if (Regex.Match(userAgentHeader, "X-Lite|X-PRO ", RegexOptions.IgnoreCase).Success)
                {
                    userAgent = SIPUserAgentsEnum.XtenSoftphone;
                }
                else if (Regex.Match(userAgentHeader, "CSCO|Cisco", RegexOptions.IgnoreCase).Success)
                {
                    userAgent = SIPUserAgentsEnum.CiscoIPPhone;
                }
                else if (Regex.Match(userAgentHeader, "TA612V", RegexOptions.IgnoreCase).Success)
                {
                    userAgent = SIPUserAgentsEnum.NetgearTA612V;
                }
                else if (Regex.Match(userAgentHeader, "PolycomSoundPoint", RegexOptions.IgnoreCase).Success)
                {
                    userAgent = SIPUserAgentsEnum.PolycomIPPhone;
                }
                else if (Regex.Match(userAgentHeader, "Grandstream GXP2000", RegexOptions.IgnoreCase).Success)
                {
                    userAgent = SIPUserAgentsEnum.GrandstreamGXP2000;
                }
                else if (Regex.Match(userAgentHeader, "Linksys/RT31P2", RegexOptions.IgnoreCase).Success)
                {
                    userAgent = SIPUserAgentsEnum.LinksysRT31P2;
                }
                else if (Regex.Match(userAgentHeader, "Sipura/SPA941", RegexOptions.IgnoreCase).Success)
                {
                    userAgent = SIPUserAgentsEnum.LinksysSPA941;
                }
                else if (Regex.Match(userAgentHeader, "SPA2100", RegexOptions.IgnoreCase).Success)
                {
                    userAgent = SIPUserAgentsEnum.Sipura2100;
                }
                else if (Regex.Match(userAgentHeader, "Siemens Gigaset", RegexOptions.IgnoreCase).Success)
                {
                    userAgent = SIPUserAgentsEnum.SiemensGigaset;
                }
                else if (Regex.Match(userAgentHeader, "Draytek", RegexOptions.IgnoreCase).Success)
                {
                    userAgent = SIPUserAgentsEnum.DraytekVigor;
                }
                else if (Regex.Match(userAgentHeader, "Asterisk", RegexOptions.IgnoreCase).Success)
                {
                    userAgent = SIPUserAgentsEnum.Asterisk;
                }
            }

            return userAgent;
        }
    }
}
